﻿using System;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

using Microsoft.Practices.Unity;
using Fuse8.DomainFramework.Common.Locator;

using Fuse8.ContentManagementFramework.Web.StartUp;
using Fuse8.ContentManagementFramework.Web.Etc;

namespace SampleSiteMvcTemplate
{
    public class MvcApplication : HttpApplication
    {
        private IUnityContainer _container;
        private UnityServiceLocatorWithLazy _locator;

        protected void Application_Start()
        {
            _container = new UnityContainer();

            _locator = new UnityServiceLocatorWithLazy(_container);
            
            DependencyServiceLocator.SetLocatorProvider(() => _locator);  
            
            GlobalFiltersPolicy.Init(GlobalFilters.Filters);

            RoutePolicy.Init(RouteTable.Routes);
        }

        protected void Application_PostReleaseRequestState(object sender, EventArgs e)
        {
            if (!Context.IsDebuggingEnabled && (Response.ContentType == "text/html"))
            {
                Response.Filter = new WhiteSpaceFilter(Response.Filter);
            }
        }

        protected void Application_EndRequest()
        {
            WebPolicy.CallClose();
        }
    }
}